<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
<?php
    include 'header.php';
  ?>
<body>


<?php
$debug=0;
$username = "fossilplot";
$password = "fossilplot";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");


//select a database to work with
$selected = mysql_select_db("fossilplot",$dbhandle)
  or die("Could not select examples");

$debug=0;
$period=$_POST['Periods'];
$level=$_POST['level'];
//echo "period is $period<br>";
//echo "level is $level<br>";

$pd_short=mysql_query("select pd_short from period where pd='$period'") or die ("error selecting from stgintv!!");
while($row_pd_short=mysql_fetch_array($pd_short)){
    $short=$row_pd_short{'pd_short'};
    //echo "short is ".$row_pd_short{'pd_short'}."<br>";

}

$phylum=mysql_query("select phylum_name from phylum") or die ("error selecting from stgintv!!");
$i=0;
while($row_phy=mysql_fetch_array($phylum)){
    $phylum_arr[$i][0]=$row_phy{'phylum_name'};
    $phylum_arr[$i][1]=0; //init. phylum_arr[$i][1]
    $i++;
}


//echo "i is $i, length is ".sizeof($phylum_arr);

if($level == "Class level"){

  $phylum=mysql_query("select phylum_name,id from phylum") or die ("error selecting from stgintv!!");
  $i=1; //phylum id starts from 1
  while($row_phy=mysql_fetch_array($phylum)){
    $phy_cl[$i]=array(array(),array()); //$i is equal the id at the same, the first array() for class names in phy_cl[i], the second array() for # of genera for related class in first array()
  
  }

  $class=mysql_query("select distinct phylum_id, class_name from pco") or die ("error selecting from class!!");
  $i=0; //start from 1 in class table, phylum id starts from 1 in "pco" table, empty class id is 0 in pco table
  while($row_cl=mysql_fetch_array($class)){
    $class_arr[$i][0]=$row_cl{'phylum_id'};
    $class_arr[$i][1]=$row_cl{'class_name'};
    $class_arr[$i][2]=0; //init. will store # of genera
    
    //$tmp1=$class_arr[$i][0];
    //$tmp2=$class_arr[$i][1];
   // echo "classe name is $tmp1, # is $tmp2<br>";
    
    $i++;
    
  }
}

  




if($short=="Pg"){ //in genus table, does not contain Pg, Ng, only T(T contains both Pg and Ng)
    $genera=mysql_query("select phylum_name, phylum_id,class_name, fad_period, lad_period from genus where (fad_period='T' and fad_date >='23.03') or (lad_period='T' and lad_date >='23.03')") or die ("error selecting from period!!");
}
else if($short =="Ng"){
        $genera=mysql_query("select phylum_name, phylum_id,class_name, fad_period, lad_period from genus where (fad_period='T' and fad_date <'23.03') or (lad_period='T' and lad_date <'23.03')") or die ("error selecting from period!!");

}
else if($short == "VC"){
        //echo "it is true!";
        $genera=mysql_query("select phylum_name,phylum_id, class_name, fad_period, lad_period from genus where fad_period='V' or fad_period ='Cm' or lad_period='V' or lad_period ='Cm'") or die ("error selecting from period!!");

}

else{
$genera=mysql_query("select phylum_name, phylum_id, class_name, fad_period, lad_period from genus where fad_period='$short' or lad_period='$short'") or die ("error selecting from stgintv!!");
}
while($row_genera=mysql_fetch_array($genera)){
     //echo "genera is ".$row_genera{'phylum_name'}."<br>";
      //if($row_genera{'fad_period'}==$short || $row_genera{'lad_period'}==$short ||(($short=="Pg" || $short=="Ng")&& ($row_genera{'fad_period'} =="T" ||$row_genera{'lad_period'} =="T")) || ($short =="VC" &&($row_genera{'fad_period'}=="Cm" ||$row_genera{'fad_period'}=="V" ||$row_genera{'lad_period'}=="Cm" ||$row_genera{'lad_period'}=="V")))
      //{
      //if($level=="Phylum level"){
        for($i=0; $i<sizeof($phylum_arr); $i++) 
            if($phylum_arr[$i][0] ==$row_genera{'phylum_name'})
                $phylum_arr[$i][1]++;
      //}
      if($level=="Class level"){      
        for($i=0; $i<sizeof($class_arr); $i++) 
            if(($class_arr[$i][1] ==$row_genera{'class_name'}) && ($class_arr[$i][0] ==$row_genera{'phylum_id'}))
                $class_arr[$i][2]++;  //count # of genera for this class in this phylum in this period 
                
      }
        
      //}
}





for($j=0; $j<sizeof($class_arr); $j++){ //total 120: distinct phylum, class from pco has 120 rows
    if($class_arr[$j][2]!=0){ //if class_arr[j][2]==0, means no genera under this class in this period, then this class will show as a small bar in the graph.
      $index=$class_arr[$j][0]; //get the phylum id
      //phy_cl[] is a 27 items array(phylum # is 27), each phy_cl[i] is the structure of (array(class1,class2,...),array(number1, number2,...)).
      $index1=sizeof($phy_cl[$index][0]);//init index1 is 0, then 1, then 2, sizeof(phy_cl[index][0])== sizeof(phy_cl[index][1]), because phy_cl[index][0] contains the class names, phy_cl[index][1] contains the RELATED # of genera for each class name in  phy_cl[index][0].
      if($class_arr[$j][1]==""){ //some phylums have no classes, using 'Blank Class' to replace
        $phy_cl[$index][0][$index1]= "Blank Class";
        $phy_cl[$index][1][$index1]= $class_arr[$j][2];
      }
      else{
        $phy_cl[$index][0][$index1]= $class_arr[$j][1];
        $phy_cl[$index][1][$index1]= $class_arr[$j][2];
      }
      
     }
  
  }
  
  $max=0; //max decide how many smail bars in the longest phylum bar
  for($i=1; $i<=27;$i++){ //total 27 phylums
    if(sizeof($phy_cl[$i][0]) >$max) $max=sizeof($phy_cl[$i][0]);
    //for($j=0; $j<sizeof($phy_cl[$i][0]);$j++){
      
      //echo "phy cl $i is\t".$phy_cl[$i][0][$j]." \t".$phy_cl[$i][1][$j]."<br>";
	  //}
  }
  
  //echo "max is $max<br>";




//for($i=0; $i<sizeof($phylum_arr); $i++) 
    //echo "arr $i is".$phylum_arr[$i][1]."<br>";
    
//for($i=0; $i<sizeof($class_arr); $i++) 
  // echo "phylum id is".$class_arr[$i][0]."\tclass is:".$class_arr[$i][1]."\t#of genera is:".$class_arr[$i][2]."<br>";


if($level == "Phylum level"){
$File = "./periodgra.php"; 
  $Handle = fopen($File, 'w');
    $Data = "<?php
include (\"./src/jpgraph.php\");
include (\"./src/jpgraph_bar.php\");
\$n = 27;
	for(\$i=0; \$i < \$n; ++\$i ) {
	    \$tmp =\$phylum_arr[\$i][0];
	    \$targ1[\$i] = \"http://www.fossilplot.org/test/new/v9/graph/getgenera.php?arg1=\$tmp&arg2=\$period\";	    
	    \$alts1[\$i] = \"number of genera=%d\";   
	}


\$datay=array";
fwrite($Handle, $Data);
$tmp =$phylum_arr[0][1];
$barval="($tmp";
for($i=1; $i<sizeof($phylum_arr); $i++){
    $barval =$barval.",".$phylum_arr[$i][1];
}

$barval=$barval.")";

$Data="$barval;
\$datax=array";
fwrite($Handle, $Data);


$tmp =$phylum_arr[0][0];
$bar_x="(\"$tmp\"";
for($i=1; $i<sizeof($phylum_arr); $i++){
    if($phylum_arr[$i][0] =="Ph. LOBOPODA (= Onychophora)"){ //there is a special phylum which is too long to be cut
        $bar_x =$bar_x.","."\""."Ph. LOBOPODA"."\"";
    }
    else{
      $bar_x =$bar_x.","."\"".$phylum_arr[$i][0]."\"";
    }
}

$bar_x=$bar_x.")";
$Data="$bar_x;

//\$datay=array(12,8,19,3,10,5);
//\$datax=array(\"ph1\",\"ph2\",\"ph3\",\"ph4\",\"ph5\",\"ph6\");

\$graph = new Graph(800,450,\"auto\");    
\$graph->SetScale(\"textlin\");
\$graph->SetShadow();
\$graph->img->SetMargin(40,30,20,130);
\$bplot = new BarPlot(\$datay);

\$bplot->SetFillColor('green');
\$bplot->value->Show();
\$graph->Add(\$bplot);

\$graph->title->Set(\"$period period graph\");
\$graph->xaxis->title->Set(\"Phylum Names\");
\$graph->yaxis->title->Set(\"Number of Genera\");
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(90);
\$graph->xaxis->title->SetMargin(90);
\$graph->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD,4);
\$bplot->SetCSIMTargets(\$targ1,\$alts1);
//\$graph->Stroke();

\$graph->Stroke('./jpgraph_cache/periodgra.png');
\$imgmap = \$graph->GetHTMLImageMap('barmap');
echo \$imgmap;
echo \"<img src='http://www.fossilplot.org/test/graph/jpgraph_cache/periodgra.png' ISMAP USEMAP='#barmap' border='0'>\";
?>
    
    
    ";
    fwrite($Handle, $Data);
 } //end of level=phylum level
 
 else{ //level is Class level
 
 
 for($i=0; $i<$max;$i++) // max decide the # of datay array in the graph
  {
  $tmpy[$i]=array(); //init datay arrays
   $alts_arr[$i]=array(); //init alts text arrays
  }
   
  for($i=0; $i<$max;$i++){
    
    for($j=1;$j<=27; $j++){
      $index=sizeof($tmpy[$i]); //in the beginning, tmpy[i] is empty,at last each tmpy[i] should be an array contains 27 numbers
      if($phy_cl[$j][1][$i]){ //if phy_cl[j][1][i] is null, meas this phy_cl length < max, 
        $tmpy[$i][$index]=$phy_cl[$j][1][$i];
        $alts_arr[$i][$index]= $phy_cl[$j][0][$i]; //the alts texts are class names for each small bar
        
      }
      else{
        $tmpy[$i][$index]=0; //using 0 to make up 27 items for each tmpy when some phy_cl length < max
      $alts_arr[$i][$index]= "";
      }
      
      
      }   
    
  }
 
 if(debug==1){
 for($i=0; $i<$max;$i++){
    echo "array $i is :";
    for($j=0;$j<30; $j++){
      echo $tmpy[$i][$j].";";
      echo "alts is ".$alts_arr[$i][$j];
      
      } 
      echo "<br>";
      
  }
}
      
  
   
 //$tmp=array(1,1,1,1,1,1);
  //echo "hello11";
  $File = "./periodgra.php"; 
  $Handle = fopen($File, 'w');
    $Data = "<?php
  include (\"./src/jpgraph.php\");
  include (\"./src/jpgraph_bar.php\");
   \$datax=array";
fwrite($Handle, $Data);
   
   $tmp =$phylum_arr[0][0];
$bar_x="(\"$tmp\"";
for($i=1; $i<sizeof($phylum_arr); $i++){
    if($phylum_arr[$i][0] =="Ph. LOBOPODA (= Onychophora)"){ //there is a special phylum which is too long to be cut
        $bar_x =$bar_x.","."\""."Ph. LOBOPODA"."\"";
    }
    else{
      $bar_x =$bar_x.","."\"".$phylum_arr[$i][0]."\"";
    }
}

$bar_x=$bar_x.")";
$Data="$bar_x;
   
   
  //\$n = 27;
	//for(\$i=0; \$i < \$n; ++\$i ) {
	    //\$targ1[\$i] = \"http://localhost/test/graph\";	    
	    //\$alts1[\$i] = \"val=%d\"; 
	    //\$alts1[\$i] = \"class1\"; 
	    
	    //\$targ2[\$i] = \"http://localhost/test/\";	    
	    //\$alts2[\$i] = \"val=%d\"; 
	    //\$alts2[\$i] = \"class2\";
	    
	    //\$targ3[\$i] = \"http://localhost/test/\";	    
	   // \$alts3[\$i] = \"val=%d\";  
	    //\$alts3[\$i] = \"class3\"; 
	//}

   
for(\$j=0; \$j<\$max;\$j++){
    \$datay[\$j]=\$tmpy[\$j];
    for(\$k=0;\$k<27;\$k++){
    \$tmp=\$alts_arr[\$j][\$k];
      \$targ[\$j][\$k] = \"http://www.fossilplot.org/test/new/v9/graph/classlevel.php?arg1=\$tmp&arg2=\$period\";	    
	    
   }
   \$alts[\$j] = \$alts_arr[\$j]; 
   
  }
   for(\$m=0;\$m<\$max;\$m++){
   for(\$n=0;\$n<27;\$n++){
   \$tmp=\$alts[\$m][\$n];
   //echo \"tmp is \$tmp\";
   }
   }
   
   //\$datay[0]=\$tmpy[0];
   //\$datay[1]=\$tmpy[1];
   //\$datay[2]=\$tmpy[2];
   //\$datay[3]=\$tmpy[3];
   
   
\$graph = new Graph(1000,650,\"auto\");    
\$graph->SetScale(\"textlin\");
\$graph->SetShadow();
\$graph->img->SetMargin(40,30,20,130);
   
for(\$j=0; \$j<\$max;\$j++)
{
   \$bplot[\$j] = new BarPlot(\$datay[\$j]);

    \$bplot[\$j]->SetFillColor('orange');
   
   
}
//\$bplot = new BarPlot(\$datay[0]);

//\$bplot->SetFillColor('orange');

//\$b2plot = new BarPlot(\$datay[1]);
//\$b2plot->SetFillColor(\"blue\");
//\$b3plot = new BarPlot(\$datay[2]);
//\$b3plot->SetFillColor(\"red\");
//\$b4plot = new BarPlot(\$datay[3]);
//\$b4plot->SetFillColor(\"green\");


// Create the grouped bar plot
\$gbplot = new AccBarPlot(array";
   fwrite($Handle, $Data);

   $gbplot_arr="(\$bplot[0]";
   for($i=1;$i< $max;$i++)
     $gbplot_arr = $gbplot_arr.","."\$bplot[$i]";
   $gbplot_arr=$gbplot_arr."))";
   $Data="$gbplot_arr;

// ...and add it to the graPH
\$graph->Add(\$gbplot);


\$graph->title->Set(\"$period Period Graph\");
\$graph->xaxis->title->Set(\"Phylum Names\");
\$graph->yaxis->title->Set(\"Number of Genera for Classes\");
";
  fwrite($Handle, $Data);    
for($i=0;$i<$max;$i++){
  $Data="
    \$bplot[$i]->SetCSIMTargets(\$targ[$i],\$alts[$i]);
    ";
   fwrite($Handle, $Data);  
  }
 $Data=" 
     
//\$bplot[0]->SetCSIMTargets(\$targ[0],\$alts[0]);
//\$bplot[1]->SetCSIMTargets(\$targ[1],\$alts[1]);
//\$bplot[2]->SetCSIMTargets(\$targ[2],\$alts[2]);
\$graph->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(90);
\$graph->xaxis->title->SetMargin(90);
\$graph->Stroke('./jpgraph_cache/periodgra.png');
\$imgmap = \$graph->GetHTMLImageMap('scattermap');
echo \$imgmap;
echo \"<img src='http://www.fossilplot.org/test/graph/jpgraph_cache/periodgra.png' ISMAP USEMAP='#scattermap' border='0'>\";
?>
  ";
  
  fwrite($Handle, $Data);
  
 
 
 
 
 
 } // end of level is Class level
    $myFile = "./jpgraph_cache/periodgra.png";
    if(file_exists($myFile)){
    unlink($myFile);
    }
 //echo "<img src=\"http://localhost/test/graph/periodgra.php\">"; 
 require('periodgra.php');

?>

<?php include 'footer.php';?>

</body>
</html>
